<?php
class Application_Model_Base_Memory_CheckCondition
{
	public static function inCondition($cond,$str){
		if (isset($cond[1]) && $cond[1] == "..")
		{
			return $str >= $cond[0] && $str <= $cond[2];
		}
		else
		{
			$result = false;
			if(!is_array($cond))
				$cond = array($cond);
			foreach ($cond as $value)
			{
				if($str == $value)
				{
					$result = true;
					break;
				}
			}
			return $result;
		}
	}
	
	public static function likeCondition($cond,$str)
	{
		$re = Application_Model_Base_Memory_CheckCondition::_makeRegex($cond);
		
		return (preg_match($re, $str));
	}

	private static function _makeRegex($cond){

		$re =array();

		$bool = false;
		for($i = 0; $i < strlen($cond);$i++)
		{
			if($cond[$i] == "%"){
				$bool = true;
				break;
			}
		}

		if($bool == false)
			$cond = "%".$cond."%";

		if($cond[0] == "%"){
			$re[count($re)] = "/(.)*";
		}else{
			$re[count($re)] = "/^";
			$re[count($re)] = $cond[0];
		}

		for( $i = 1; $i < strlen($cond) - 1; $i++)
		{
			if($cond[$i] == "%"){
				$re[count($re)] = "(.)*";
			}else{
				$re[count($re)] = $cond[$i];
			}
		}

		if($cond[strlen($cond)-1] == "%"){
			$re[count($re)] = "(.)*/";
		}else{
			$re[count($re)] = $cond[strlen($cond)-1];
			$re[count($re)] = "$/";
		}

		$re = implode($re);

		return $re;
	}
}
